package com.ansion.commonchat.startup.workmanager;


import com.ansion.commonchat.BaseApplication;
import com.ansion.commonchat.startup.workmanager.bwork.BWorkInfo;
import com.ansion.commonchat.startup.workmanager.bwork.BWorkManager;
import com.ansion.log.LogTag;
import com.ansion.log.VLog;
import com.ansion.utils.GsonUtil;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.List;
import java.util.UUID;

/**
 * <pre>
 *     author : jake
 *     e-mail : hongjie.wang@ctechm.com
 *     time   : 2022/12/29 15：44
 *     function   : WorkManager 工具类
 * </pre>
 */
public class WorkManagerUtils {
    /**
     * 传入 work id，查询此 work 是否完成
     * 注意，调用此方法，之前先调用 process 方法。
     *
     * @param id Work Id
     * @return 默认为 true
     */
    public static boolean isWorkFinishedById(UUID id) {
        ListenableFuture<BWorkInfo> info = BWorkManager.getInstance(BaseApplication.getContext()).getWorkInfoById(id);
        try {
            BWorkInfo workInfo = info.get();
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkFinishedById , getState  %s , getId %s ,getTags %s ", workInfo.getState(), workInfo.getId(), workInfo.getTags());
            }
            return workInfo.getState().isFinished();
        } catch (Exception e) {
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkFinishedById catch >> %s ", GsonUtil.GsonString(e));
            }
            return true;
        }
    }

    /**
     * Pass in the work tag to check whether the batch work is completed
     * Note that before calling this method, call the process method first.
     *
     * @param tag Work tag
     * @return defaults to true
     */
    public static boolean isWorkFinishedByTag(String tag) {
        ListenableFuture<List<BWorkInfo>> infoList = BWorkManager.getInstance(BaseApplication.getContext()).getWorkInfosByTag(tag);
        try {
            List<BWorkInfo> workInfoList = infoList.get();
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkFinishedByTag , workInfoList size %s ", workInfoList.size());
            }
            for (BWorkInfo workInfo : workInfoList) {
                if (VLog.OPEN_LOG) {
                    VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkFinishedByTag , getState  %s , getId %s ,getTags %s ", workInfo.getState(), workInfo.getId(), workInfo.getTags());
                }
                if (!workInfo.getState().isFinished()) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkFinishedByTag catch >> %s ", GsonUtil.GsonString(e));
            }
            return true;
        }
    }

    /**
     * Get batch tasks based on Tag, and the tasks obtained have been scheduled and completed
     */
    public static boolean isWorkBeginAndFinishedByTag(String tag) {
        ListenableFuture<List<BWorkInfo>> infoList = BWorkManager.getInstance(BaseApplication.getContext()).getWorkInfosByTag(tag);
        try {
            List<BWorkInfo> workInfoList = infoList.get();
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkBeginAndFinishedByTag , workInfoList size %s ", workInfoList.size());
            }
            if (workInfoList == null || workInfoList.isEmpty()) {
                return false;
            }
            for (BWorkInfo workInfo : workInfoList) {
                if (VLog.OPEN_LOG) {
                    VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkBeginAndFinishedByTag , getState  %s , getId %s ,getTags %s ", workInfo.getState(), workInfo.getId(), workInfo.getTags());
                }
                if (!workInfo.getState().isFinished()) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_WORK_MANGER, " WorkManagerUtils isWorkBeginAndFinishedByTag catch >> %s ", GsonUtil.GsonString(e));
            }
            return true;
        }
    }

}
